import css from './module.css'
import {Vue,axios,navigation,commonVue,loadsection} from '../../assets/js/base.js';
import SYS_Module_sel from  '../../API/SYS_Module_sel.js';
import SYS_Module_update from  '../../API/SYS_Module_update.js';
import SYS_Module_del from  '../../API/SYS_Module_del.js';
import upload from  '../../component/upload';
const config = require('../../../config');
const imgUploadPath = !!process.env  ? config.dev.imgUploadPath : config.build.imgUploadPath;
const imgPath = !!process.env  ? config.dev.imgPath : config.build.imgPath;



new Vue({
    el: '#main',
    data:{
      fullScreen:false,//是否全屏
      currentMenu:"module.html",//当前菜单
      imgUploadPath:imgUploadPath,
      imgPath:imgPath,
      tableLoading:false,
      pageState:false,
      loadSection:false,
      table_data_total:0,
      table_data:[],
      currentID:"",
      currentName:"",
      bannerTitle:"滚动图片",
      comAttr:"公司介绍",
      comAttrValue:"",
      teamAttrOne:"团队介绍1",
      teamAttrOneCardName:"",
      teamAttrOneRoleName:"",
      teamAttrOneTelName:"",
      teamAttrOneMailName:"",
      teamAttrTwo:"团队介绍2",
      teamAttrTwoCardName:"",
      teamAttrTwoPositionName:"",
      link:"横向的三张图",
      zhaopin:"招聘",
      zhaopinName:"",
      zhaopinSalary:"",
      zhaopinYear:"",
      zhaopinGrade:"",
      comAddress:"公司地址",
      comAddressValue:"",
      justTitle:"",
      contactType:"联系方式",
      contactTypeValue:"",
      oneLinkImg:"一张有链接的图片",
      oneLinkImgContent:"",
      oneLinkImgHeight:"",
      ask:"咨询",
      askText:"",
      askTime:"",
      justAttr:"",

      justAttrState:false,
      askState:false,
      oneLinkImgState:false,
      contactTypeState:false,
      justTitleState:false,
      comAddressState:false,
      zhaopinState:false,
      linkState:false,
      teamAttrTwoState:false,
      teamAttrOneState:false,
      comAttrState:false,
      bannerTitleState:false,
      oneLinkImgShow:false,
      oneLink:""

    },
    mounted:function(){
      this.getData();
    },
    methods:{
      globalEvent:function(){
        commonVue.$emit('navSelected');
      },
      getData(){
        this.tableLoading = true;

        let _promise = [];

        let parm0 = {Type:0};
        let parm1 = {Type:1};
        let parm7 = {Type:7};
        let parm4 = {Type:4};
        let parm5 = {Type:5};
        let parm6 = {Type:6};
        let parm8 = {Type:8};
        let parm9 = {Type:9};
        let parm15 = {Type:15};
        let parm17 = {Type:17};
        let parm25 = {Type:25};
        let parm27 = {Type:27};
        _promise.push(SYS_Module_sel(parm0));
        _promise.push(SYS_Module_sel(parm1));
        _promise.push(SYS_Module_sel(parm7));
        _promise.push(SYS_Module_sel(parm4));
        _promise.push(SYS_Module_sel(parm5));
        _promise.push(SYS_Module_sel(parm6));
        _promise.push(SYS_Module_sel(parm8));
        _promise.push(SYS_Module_sel(parm9));
        _promise.push(SYS_Module_sel(parm15));
        _promise.push(SYS_Module_sel(parm17));
        _promise.push(SYS_Module_sel(parm25));
        _promise.push(SYS_Module_sel(parm27));

        Promise.all(_promise).then((result) => {


          this.setBanner(result[0]);
          this.setcomAttr(result[1]);
          this.setTeamOne(result[2]);
          this.setTeamTwo(result[3]);
          this.setThree(result[4]);
          this.setZhaopin(result[5]);
          this.setComAddress(result[6]);
          this.setJustTitle(result[7]);
          this.setContactType(result[8]);
          this.setOneLinkImg(result[9]);
          this.setAsk(result[10]);
          this.setAttr(result[11]);

          this.tableLoading = false;

        }).catch((error) => {
          console.log(error)
          this.tableLoading = false;
        })


      },
      confirm(){
        SYS_Module_del({}).then(data => {
          this.addBanner();
          this.addcomAttr();
          this.addTeamOne();
          this.addTeamTwo();
          this.addThree();
          this.addZhaopin();
          this.addComAddress();
          this.addJustTitle();
          this.addContactType();
          this.addOneLinkImg();
          this.addAsk();
          this.addAttr();



        }).catch(message => {
          console.log("SYS_Module_del异常:",message)
          this.$Message({
              message
          });
        });


      },
      setAttr(data){
        this.justAttr = data.text;
        this.justAttrState = data.show;
      },
      addAttr(){
        let parm = {
          Type:27,
          Sno:1,
          show:this.justAttrState,
          text:this.justAttr,
        };
        SYS_Module_update(parm);
      },
      setAsk(data){
        if(!!data.name){
          this.ask = data.name;
        }
        this.askState = data.show;

        this.askText = data.text;
        this.askTime = data.time;
        if(!!data.img){
          this.$refs.uploadTalk.initData([data.img])
        }

      },
      addAsk(){
        this.getImgData("uploadTalk",(data)=>{
          let _img = "";
          if(data.length > 0){
            _img = data[0];
          }
          let parm = {
            Type:25,
            Sno:1,
            show:this.askState,
            name:this.ask,
            img:_img,
            text:this.askText,
            time:this.askTime,
          };
          SYS_Module_update(parm);
        });
      },
      setOneLinkImg(data){
        if(!!data.name){
          this.oneLinkImg = data.name;
        }
        this.oneLinkImgState = data.show;

        this.oneLinkImgShow = (data.text1 == "1") ? true : false;
        this.oneLink = data.text2;


        this.oneLinkImgContent = data.text;
        this.oneLinkImgHeight = data.height;
        if(!!data.img){
          this.$refs.uploadLink.initData([data.img]);
        }

      },
      addOneLinkImg(){
        this.getImgData("uploadLink",(data)=>{
          let _img = "";
          if(data.length > 0){
            _img = data[0];
          }
          let parm = {
            Type:17,
            Sno:1,
            show:this.oneLinkImgState,
            name:this.oneLinkImg,
            img:_img,
            text:this.oneLinkImgContent,
            height:this.oneLinkImgHeight,
            text1:!!this.oneLinkImgShow ? 1 : 0,
            text2:this.oneLink
          };
          SYS_Module_update(parm);
        });
      },
      setContactType(data){
        if(!!data.name){
          this.contactType = data.name;
        }
        this.contactTypeState = data.show;

        this.contactTypeValue = data.text;
      },
      addContactType(){
        let parm = {
          Type:15,
          Sno:1,
          show:this.contactTypeState,
          name:this.contactType,
          text:this.contactTypeValue,
        };
        SYS_Module_update(parm);
      },
      setJustTitle(data){
        if(!!data.name){
          this.justTitle = data.name;
        }
        this.justTitleState = data.show;

      },
      addJustTitle(){
        let parm = {
          Type:9,
          Sno:1,
          show:this.justTitleState,
          name:this.justTitle,
        };
        SYS_Module_update(parm);
      },
      setComAddress(data){
        if(!!data.name){
          this.comAddress = data.name;
        }
        this.comAddressState = data.show;

        this.comAddressValue = data.text;
      },
      addComAddress(){
        let parm = {
          Type:8,
          Sno:1,
          show:this.comAddressState,
          name:this.comAddress,
          text:this.comAddressValue,
        };
        SYS_Module_update(parm);
      },
      setZhaopin(data){
        if(!!data.name){
          this.zhaopin = data.name;
        }
        this.zhaopinState = data.show;

        this.zhaopinName = data.text1;
        this.zhaopinSalary = data.text2;
        this.zhaopinYear = data.text3;
        this.zhaopinGrade = data.text4;
      },
      addZhaopin(){
        let parm = {
          Type:6,
          Sno:1,
          name:this.zhaopin,
          show:this.zhaopinState,
          text1:this.zhaopinName,
          text2:this.zhaopinSalary,
          text3:this.zhaopinYear,
          text4:this.zhaopinGrade,
        };
        SYS_Module_update(parm);

      },
      setThree(data){
        if(!!data.name){
          this.link = data.name;
        }
        this.linkState = data.show;

        if(!!data.img){
          let _img = data.img.split(';');
          this.$refs.uploadThree.initData(_img);
        }

      },
      addThree(){
        this.getImgData("uploadThree",(data)=>{
          let img = "";
          for(let i = 0; i < data.length; i++){
            img = img + data[i] + ";";
          }
          let parm = {
            Type:5,
            Sno:1,
            show:this.linkState,
            name:this.link,
            img:img
          };
          SYS_Module_update(parm);

        });
      },
      setTeamTwo(data){
        if(!!data.name){
          this.teamAttrTwo = data.name;
        }
        this.teamAttrTwoState = data.show;

        this.teamAttrTwoCardName = data.text2;
        this.teamAttrTwoPositionName = data.text3;
        if(!!data.img){
          this.$refs.uploadTeamTwo.initData([data.img]);
        }

      },
      addTeamTwo(){
        this.getImgData("uploadTeamTwo",(data)=>{
          let _img = "";
          if(data.length > 0){
            _img = data[0];
          }
          let parm = {
            Type:4,
            Sno:1,
            name:this.teamAttrTwo,
            img:_img,
            show:this.teamAttrTwoState,
            text2:this.teamAttrTwoCardName,
            text3:this.teamAttrTwoPositionName,
          };
          SYS_Module_update(parm);
        });
      },
      setTeamOne(data){
        if(!!data.name){
          this.teamAttrOne = data.name;
        }

        this.teamAttrOneState = data.show;

        this.teamAttrOneCardName = data.text2;
        this.teamAttrOneRoleName = data.text3;
        this.teamAttrOneTelName = data.text4;
        this.teamAttrOneMailName = data.text5;
        if(!!data.img){
          this.$refs.uploadTeamOne.initData([data.img])
        }

      },
      addTeamOne(){
        this.getImgData("uploadTeamOne",(data)=>{
          let _img = "";
          if(data.length > 0){
            _img = data[0];
          }

          let parm = {
            Type:7,
            Sno:1,
            name:this.teamAttrOne,
            img:_img,
            show:this.teamAttrOneState,
            text2:this.teamAttrOneCardName,
            text3:this.teamAttrOneRoleName,
            text4:this.teamAttrOneTelName,
            text5:this.teamAttrOneMailName,
          };
          SYS_Module_update(parm);

        });
      },
      setcomAttr(data){
        if(!!data.name){
          this.comAttr = data.name;
        }
        this.comAttrState = data.show;

        this.comAttrValue = data.text;
      },
      addcomAttr(){
        let parm = {
          Type:1,
          Sno:1,
          show:this.comAttrState,
          name:this.comAttr,
          text:this.comAttrValue
        };
        SYS_Module_update(parm);
      },
      setBanner(data){
        if(!!data.name){
          this.bannerTitle = data.name;
        }

        this.bannerTitleState = data.show;

        if(!!data.banner){
          let _img = data.banner.split(';');
          this.$refs.uploadBanner.initData(_img);
        }

      },
      addBanner(){
        this.getImgData("uploadBanner",(data)=>{

          let banner = "";
          for(let i = 0; i < data.length; i++){
            banner = banner + data[i] + ";";
          }

          let parm = {
            Sno:1,
            Type:0,
            show:this.bannerTitleState,
            name:this.bannerTitle,
            banner:banner
          };
          SYS_Module_update(parm);

        });
      },
      getImgData(ref,callback){
        this.$refs[ref].upload(data => {
          if(data.res == "0"){//出错了
            alert(data.mes)
            return;
          }

          let _data = data.imgList;//这个地方是用户新上传的图片地址
          let oldImgpath = data.oldImgpath;//这个地方是编辑的时候传进去的图片地址
          let imgPathList = [];
          for(let i = 0; i < _data.length; i++){
            imgPathList.push(_data[i].value)//保存图片地址
          }
          for(let i = 0; i < oldImgpath.length; i++){
            imgPathList.push(oldImgpath[i])//保存图片地址
          }
          callback(imgPathList);
        });
      },
      checkPermission(){
        return true;
      },
    },
    components: {
      navigation,
      loadsection,
      upload
    }
})
